package com.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.CheckGroup;
import com.health.service.CheckGroupService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * t_checkGroup表
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;
    /**
     * 查询检查组
     */
    @GetMapping("findByQuery")
    public Result findByQuery(String query){

        List<CheckGroup> checkGroupList=checkGroupService.findByQuery(query);

        return new Result(true,"检查组查询成功",checkGroupList);
    }

    /**
     * 编辑检查组3-根据id编辑修改检查组和关联的检查项
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        int count=checkGroupService.update(checkGroup,checkItemIds);

        if (count>0){
            return new Result(true,"编辑检查组成功");
        }

        return new Result(false,"编辑检查组失败");
    }

    /**
     * 编辑检查组2-根据id查询检查组关联的检查项id,数据回显
     */
    @GetMapping("/findCheckItemIds/{id}")
    public Result findCheckItemIds(@PathVariable Integer id){
        List<Integer> checkItemIds=checkGroupService.findCheckItemIds(id);

        return new Result(true,"查询检查组关联的检查项id成功",checkItemIds);
    }

    /**
     * 编辑检查组1-根据id查询检查组,数据回显
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        CheckGroup checkGroup=checkGroupService.findById(id);

        return new Result(true,"查询检查组成功",checkGroup);
    }


    /**
     * 添加检查组
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        int count=checkGroupService.add(checkGroup,checkItemIds);

        if (count>0){
            return new Result(true,"添加检查组成功");
        }

        return new Result(false,"添加检查组失败");
    }

    /**
     * 删除检查组
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        int count=checkGroupService.deleteById(id);

        if (count>0){
            return new Result(true,"删除检查组成功");
        }
        return new Result(false,"删除检查组失败");
    }

    /**
     *检查组分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow,@PathVariable Integer pageSize,String query){
        PageResult<CheckGroup> pageResult=checkGroupService.findPage(pageNow,pageSize,query);

        return new Result(true,"检查组分页查询成功",pageResult);
    }
}
